<template>
    <div class="_page other-video-page">
        <div class="video-container" style="opacity: 0;">
            <video ref="thisVideo" autoplay :src="videoUrl"></video>">
        </div>
        <div class="text-layer" :class="{ active: !showTextLayer }">
            <h1>聪明的宝子耐心看下去</h1>
        </div>
        <div class="text-layer" :class="{ active: showTextLayer }">
            <h1>Yu Tong</h1>
            <h2>这儿法不了文件</h2>
            <h3>v：yyt69358</h3>
        </div>
        <button @click="doStartOrEnd()">开始/结束</button>

        <div class="spec">
            <div>
                <h2 data-content="+V">+V</h2>
                <h2 data-content="yyt69358">yyt69358</h2>
            </div>
        </div>
    </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
export default {
    setup () {
        const route = useRoute()
        const thisVideo = ref(null)
        const showTextLayer = ref(false)
        let mediaRecorder = null
        let isTag = false
        let isStart = false
        const videoUrl = ref('https://czc-tiles.obs.cn-east-3.myhuaweicloud.com/oms/001.mp4')

        const doStartRecord = async () => {
            const displayMediaOptions = {
                video: { cursor: 'always' },
                audio: { echoCancellation: true, noiseSuppression: true, sampleRate: 44100 },
            }
            const captureStream = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions)
            const options = { mimeType: 'video/webm;codecs=vp9' }
            mediaRecorder = new MediaRecorder(captureStream, options)
            const recordedChunks = []
            let count = 0
            mediaRecorder.ondataavailable = (event) => {
                count++
                if (event.data.size > 0) {
                    recordedChunks.push(event.data)
                }
            }
            mediaRecorder.onstart = () => {
                thisVideo.value.play()
                setTimeout(() => {
                    showTextLayer.value = true
                }, 1200)
            }
            mediaRecorder.onstop = () => {
                const blob = new Blob(recordedChunks, { type: 'video/webm' })
                const url = URL.createObjectURL(blob)

                // 下载
                const a = document.createElement('a')
                a.href = url
                a.download = `screen_recording_${Date.now()}.webm`
                document.body.appendChild(a)
                a.click()
                document.body.removeChild(a)
            }
            mediaRecorder.start()

            // setTimeout(() => {
            //     mediaRecorder.stop()
            // }, 5500)
        }

        const doStartOrEnd = () => {
            if (isStart) {
                mediaRecorder.stop()
            } else {
                doStartRecord()
            }
            isStart = !isStart
        }

        const doInit = () => {
            if (isTag) {
                thisVideo.value.play()
                setTimeout(() => {
                    showTextLayer.value = true
                }, 4200)
            } else {
                doStartRecord()
            }
        }

        onMounted(() => {
            isTag = route.query.t == 1
        })

        return {
            videoUrl,
            thisVideo,
            showTextLayer,
            doInit,
            doStartOrEnd
        }
    }
}
</script>

<style lang="scss">
    @use "./style.scss";
</style>